﻿#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

namespace com.yoozoo.gta
{
    public class FishingDrawLine : MonoBehaviour
    {
        [Header("最小抛竿距离")]
        public float minThrowDistance = 4;
        [Header("最大抛竿距离")]
        public float maxThrowDistance = 14;
        [Header("最大旋转角度")]
        public float maxRotateAngle = 20;
        [Header("完美圈半径(也是后台需要用到的鱼圈半径)")]
        public float perfectRadius = 2f;
        [Header("很棒圈半径(纯前台表现用到)")]
        public float goodRadius = 2f;
        private int segments = 50;

        private void OnDrawGizmos()
        {
            Vector3[] vertex = GetVertex();
            Gizmos.color = Color.yellow;
            Gizmos.DrawLine(vertex[0], vertex[1]);
            Gizmos.DrawLine(vertex[1], vertex[3]);
            Gizmos.DrawLine(vertex[3], vertex[2]);
            Gizmos.DrawLine(vertex[2], vertex[0]);

            Vector3[] realVertex = GetRealVertex();
            Gizmos.color = Color.green;
            Gizmos.DrawLine(realVertex[0], realVertex[1]);
            Gizmos.DrawLine(realVertex[1], realVertex[3]);
            Gizmos.DrawLine(realVertex[3], realVertex[2]);
            Gizmos.DrawLine(realVertex[2], realVertex[0]);

            Gizmos.color = Color.green;
            DrawCircle(perfectRadius);
            Gizmos.color = Color.blue;
            DrawCircle(goodRadius);
        }

        private void DrawCircle(float radius)
        {
            float angle = 0f;
            float angleStep = 2f * Mathf.PI / segments;
            Vector3 prevPoint = Vector3.zero;
            for (int i = 0; i <= segments; i++)
            {
                float x = radius * Mathf.Cos(angle);
                float y = radius * Mathf.Sin(angle);
                Vector3 currentPoint = transform.position + new Vector3(x, 0f, y);
                if (i > 0)
                {
                    Gizmos.DrawLine(prevPoint, currentPoint);
                }
                prevPoint = currentPoint;
                angle += angleStep;
            }
        }

        public Vector3[] GetVertex()
        {
            Vector3 startPoint = Vector3.zero;
            float angle2Rad = maxRotateAngle * Mathf.Deg2Rad;
            float minX, minZ, maxX, maxZ;
            minX = startPoint.x + Mathf.Sin(angle2Rad) * minThrowDistance;
            minZ = startPoint.z + Mathf.Cos(angle2Rad) * minThrowDistance;
            maxX = startPoint.x + Mathf.Sin(angle2Rad) * maxThrowDistance;
            maxZ = startPoint.z + Mathf.Cos(angle2Rad) * maxThrowDistance;

            Vector3[] vertex = new Vector3[4];
            vertex[0] = new Vector3(-minX, transform.position.y, minZ);
            vertex[1] = new Vector3(minX, transform.position.y, minZ);
            vertex[2] = new Vector3(-maxX, transform.position.y, maxZ);
            vertex[3] = new Vector3(maxX, transform.position.y, maxZ);
            return vertex;
        }

        public Vector3[] GetRealVertex()
        {
            // 真实服务器鱼圈刷新范围顶点，左下右下为最小抛竿距离，要舍弃掉部分距离，因为同一个长度的向量，基于原点进行不同角度旋转，所得到的最终点都不是在一条水平线的
            Vector3[] vertex = GetVertex();
            vertex[0].z = minThrowDistance;
            vertex[1].z = minThrowDistance;
            return vertex;
        }
    }

    [CustomEditor(typeof(FishingDrawLine))]
    public class FishingDrawLineEditor : Editor
    {
        FishingDrawLine drawLine;

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (drawLine == null)
            {
                drawLine = base.target as FishingDrawLine;
            }
            if (GUILayout.Button("打印范围数据"))
            {
                Vector3[] realVertex = drawLine.GetRealVertex();
                float radius = drawLine.perfectRadius;
                Debug.LogFormat("\n 顶点:[左下角:({0},{1}),右下角:({2},{3}),左上角:({4},{5}),右上角:({6},{7})]，圆半径:{8}", realVertex[0].x.ToString("0.00"), realVertex[0].z.ToString("0.00"), realVertex[1].x.ToString("0.00"), realVertex[1].z.ToString("0.00"), realVertex[2].x.ToString("0.00"), realVertex[2].z.ToString("0.00"), realVertex[3].x.ToString("0.00"), realVertex[3].z.ToString("0.00"), radius.ToString("0.00"));
            }
        }
    }
}
#endif